package com.auth.control.auth;


import com.auth.control.model.RoleAuthDto;
import com.auth.domain.module.vo.authmanage.AuthVO;
import com.auth.domain.service.IRoleAuthService;
import com.redis.sdk.cachecore.CacheLocal;
import com.redis.sdk.cachecore.CacheLocalUpdate;
import com.redis.sdk.cachecore.CacheRedis;
import com.redis.sdk.cachecore.CacheRedisUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色权限表(角色绑定权限相关) 前端控制器
 * </p>
 *
 * @author cyj
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/role/auth/{roleId}")
@Slf4j
@Api(tags = "角色关联权限")
public class RoleAuthController {

    @Autowired
    private IRoleAuthService roleAuthService;

    @GetMapping("/all")
    @ApiOperation("获取角色的所有权限")
    @CacheRedis("#roleId")
    @CacheLocal("#roleId")
    public List<AuthVO> getRoleAuths(@PathVariable Long roleId) {
        List<AuthVO> authVos= roleAuthService.getRoleAuths(roleId);
        return authVos;
    }

    @PostMapping
    @ApiOperation("保存角色所有的权限")
    @CacheRedisUpdate("#roleId")
    @CacheLocalUpdate("#roleId")
    public List<AuthVO> saveRoleAuths(@PathVariable Long roleId,
                                          @RequestBody List<RoleAuthDto> roleAuthDtos) {
        roleAuthService.saveRoleAuths(roleId, roleAuthDtos);
        return roleAuthService.getRoleAuths(roleId);
    }
}

